<?php
/**
 * Notes:
 * User: Administrator
 * Date: 2020/10/28
 * Time: 13:20
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 */

namespace App\Http\Controllers;


use App\Http\Controllers\Dsc\Model\Orderinfo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class PollController extends Controller
{


    public function callbackurl(Request $request)
    {
        $param = $request->post('param');
        $insertparam = $param;
        Log::info("===========回调函数接收初始数据第二次=========" . json_encode($param));
        $param = json_decode(str_replace(" ", "", $param));
        Log::info("===========回调函数param无空格=========" . json_encode($param));
        $data['status'] = $param->status;
        $data['message'] = $param->message;
        $data['autoCheck'] = isset($param->autoCheck) ? $param->autoCheck : 2;

        $data['state'] = $param->lastResult->state;
        $data['ischeck'] = $param->lastResult->ischeck;
        $data['com'] = $param->lastResult->com;
//        $data['data'] = json_encode($param->lastResult->data);
        $data['data'] = $insertparam;

        Log::info("===========回调函数接收数据第三次=========" . json_encode($data));
        $state = DB::table('orderdetail')->where('expressnum', $param->lastResult->nu)->first();
        $state = json_decode(json_encode($state), true);
        if (empty($state['expressnum'])) {
            Log::info('====expressnum是空=====' . $param->lastResult->nu);
        }
        Log::info('====state1=====' . $param->lastResult->state);
        if ($param->lastResult->state == 3) {
            Log::info('====state=====' . $param->lastResult->state);
            $res2 = DB::table('orderdetail')->where('expressnum', $param->lastResult->nu)
                ->update(['orStatus' => 9]);//如果已经签收那就修改子订单状态为==已完成
            Log::info('====$res2=====' . $res2);
        }

        if ($state['exprestatus'] == $param->lastResult->state) {

            $res1 = DB::table('poll')->where('nu', $param->lastResult->nu)
                ->update($data);
            Log::info('==poll更新结果1====' . $res1);

            if ($res1 == 1) {
                return response()->json(['result' => 'true', 'returnCode' => '200', 'message' => '提交成功']);
            } else {
                return response()->json(['result' => 'false', 'returnCode' => '500', 'message' => '服务器错误']);
            }
        } else {

            DB::beginTransaction();
            try {

                $res1 = DB::table('poll')->where('nu', $param->lastResult->nu)
                    ->update($data);
                Log::info('==poll更新结果2====' . $res1);

                $res2 = DB::table('orderdetail')->where('expressnum', $param->lastResult->nu)
                    ->update(['exprestatus' => $param->lastResult->state]);

                Log::info('===orderdetail运单号更新结果==' . $res2);

                if ($res1 == 1 && $res2 == 1) {
                    DB::commit();
                    return response()->json(['result' => 'true', 'returnCode' => '200', 'message' => '提交成功']);
                } else {
                    DB::rollback();//事务回滚
                    return response()->json(['result' => 'false', 'returnCode' => '500', 'message' => '服务器错误']);
                }

            } catch (\Exception $e) {
                DB::rollback();//事务回滚
//             echo $e->getMessage();
                //$result = $e->getCode();
                return response()->json(['result' => 'false', 'returnCode' => '500', 'message' => '失败']);

            }

        }
    }

    public function dsccallbackurl(Request $request)
    {
        $param = $request->post('param');
        $insertparam = $param;
        Log::info("===========dsc回调1=========" . json_encode($param));
        $state = mysql_cont('order_info')->where('invoice_no', $param->lastResult->nu)->exists();
        if ($state == false) {
            Log::info('====运单号不存在=====' . $param->lastResult->nu);
            return response()->json(['code' => '40000', 'message' => 'System Error', 'subcode' => 'GSE.SYSTEM_ERROR', 'submessage' => '[20887]服务异常请稍后再试运单号不存在']);
        }
        $update_data = ['data' => $insertparam];
        $where = ['invoice_no' => $param->lastResult->nu];
        $upd_res = Orderinfo::update_order($where, $update_data);
        if ($upd_res == 1) {
            return response()->json(['code' => '10000', 'message' => 'Success', 'submessage' => '发货成功']);
        } else {
            return response()->json(['code' => '40000', 'message' => 'System Error', 'subcode' => 'GSE.SYSTEM_ERROR', 'submessage' => '[20887]服务异常请稍后再试运单号不存在']);
        }

    }




}
